import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:honeypotflutter/modules/home/page/home/view.dart';
import 'package:honeypotflutter/modules/mine/page/mine/view.dart';
import 'package:honeypotflutter/public/views/public_keepAlive.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';
import 'package:honeypotflutter/router/bussine_router_url.dart';
import 'logic.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({Key? key}) : super(key: key);

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  int _selectIndex = 0;
  late PageController _pageController = PageController(initialPage: _selectIndex);

  List<TabItem> _items = [
    TabItem(
        icon: Image.asset(MGImage.image(
          "tabBar_home",
          type: ImagePathType.tab,
        )),
        activeIcon: Image.asset(MGImage.image("tabBar_homeSelect", type: ImagePathType.tab)),
        title: '首页'),
    TabItem(
      icon: GestureDetector(
        child: Image.asset(MGImage.image(
          "tabBar_scanfs",
          type: ImagePathType.tab,
        )),
        onTap: () {
          Get.toNamed(MGRouterUrl.deliver_scanf);
        },
      ),
      activeIcon: Image.asset(MGImage.image(
        "tabBar_scanfs",
        type: ImagePathType.tab,
      )),
    ),
    TabItem(
        icon: Image.asset(MGImage.image(
          "tabBar_mine",
          type: ImagePathType.tab,
        )),
        activeIcon: Image.asset(MGImage.image(
          "tabBar_mineSelect",
          type: ImagePathType.tab,
        )),
        title: '我的'),
  ];

  int _middle = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _middle = (_items.length / 2).floor();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: ConvexAppBar(
        initialActiveIndex: _selectIndex,
        style: TabStyle.fixedCircle,
        color: MGColors.text1Color,
        activeColor: MGColors.text1Color,
        backgroundColor: MGColors.tabBarColor,
        items: _items,
        curveSize: 80,
        onTap: (int index) {
          _selectIndex = index;
          if (index < _middle) {
            _pageController.jumpToPage(index);
            return;
          }
          _pageController.jumpToPage(index - 1);
        },
      ),
      body: PageView(
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: const [HLKeepAlive(child: HomePage()), HLKeepAlive(child: MinePage())],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<TabBarLogic>();
    super.dispose();
  }
}
